<template>
  <div class="example-container">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item
        v-for="(item, index) in breadList"
        :key="index"
        :to="item.path"
      >
        {{ item.meta.title }}
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "Breadcrumb",
  data() {
    return {
      breadList: [],
    };
  },
  watch: {
    $route() {
      this.getBreadcrumb();
    },
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    isLayout(route) {
      return route.name === "Layout";
    },
    getBreadcrumb() {
      let matched = this.$route.matched;
      //剔除布局路由（即根路由"/"）
      if (this.isLayout(matched[0])) {
        matched = [].concat(matched);
        matched.splice(0, 1, { path: "/home", meta: { title: "首页" } });
      }
      this.breadList = matched;
    },
  },
};
</script>
